<script setup>
//导入 element-plus
import { ElMessage, ElMessageBox } from 'element-plus'
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom,
    Collection,
    Document,
    SetUp,
    Avatar
} from '@element-plus/icons-vue'

//导入 有Pinia函数的 userInfo.js文件
import useUserInfoStore from '@/stores/userInfo.js'
const userInfoStore = useUserInfoStore();

//导入 有 Pinia 函数的 token.js文件
import { useTokenStore } from '@/stores/token.js'
const tokenStore = useTokenStore();


//导入路由
import { useRouter } from 'vue-router'
const router = useRouter();

//导入 获取用户详细信息
import { userInfoService } from '@/api/user.js'
//定义获取用户详细信息的函数
const getUserInfo = async () => {
    //调用接口:获取用户详细信息
    let result = await userInfoService();
    //数据存储到pinia中
    userInfoStore.setInfo(result.data);
}
//主动调用一下刚才创建的函数
getUserInfo();


//条目被点击后,调用的函数
const handleCommand = (command) => {
    //判断指令
    if (command === 'logout') {
        //退出登录
        ElMessageBox.confirm(
            '您确认要退出吗?',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(async () => {
                //退出登录
                //1.清空pinia中存储的token以及个人信息
                tokenStore.removeToken()
                userInfoStore.removeInfo()

                //2.跳转到登录页面
                router.push('/login')
                ElMessage({
                    type: 'success',
                    message: '退出登录成功',
                })

            })
            .catch(() => {
                ElMessage({
                    type: 'info',
                    message: '用户取消了退出登录',
                })
            })
    } else {
        //使用路由 Router 切换组件，完成跳转
        router.push('/user/' + command)
    }
}
</script>

<template>
    <!-- element-plus中的容器 -->
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo">big-event</div>
            <!-- element-plus的菜单标签 -->
            <el-menu active-text-color="#ffd04b" background-color="#232323" text-color="#fff" router>
                <!-- 这里的index的作用，当点击文章分类时，
                 需要展示的组件对应的路径，该路径要跟路由里写的路径保持一直 -->
                <el-menu-item index="/article/category">
                    <el-icon>
                        <Collection />
                    </el-icon>
                    <span>文章分类</span>
                </el-menu-item>
                <el-menu-item index="/article/manage">
                    <el-icon>
                        <Document />
                    </el-icon>
                    <span>文章管理</span>
                </el-menu-item>

                <el-sub-menu>
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>个人中心</span>
                    </template>
                    <el-menu-item index="/user/info">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>基本资料</span>
                    </el-menu-item>
                    <el-menu-item index="/user/avatar">
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>更换头像</span>
                    </el-menu-item>
                    <el-menu-item index="/user/resetPassword">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>重置密码</span>
                    </el-menu-item>
                </el-sub-menu>
                
                <el-sub-menu>
                    <template #title>
                        <el-icon>
                            <SetUp />
                        </el-icon>
                        <span>系统管理</span>
                    </template>
                    <el-menu-item index="/sys/menu">]
                        <el-icon>
                            <Management />
                        </el-icon>
                        <span>菜单管理</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <!-- 用插值表达式{{}} 把 Pinia 中的数据渲染出来 -->
                <div>用户昵称：<strong>{{ userInfoStore.info.nickname }}</strong></div>
                <!-- 下拉菜单 -->
                <!-- command: 条目被点击后会触发,在事件函数上可以声明一个参数,接收条目对应的指令 -->
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <!-- el-avatar：头像
                            这里用的 三目运算，若有用户头像，就用该头像，
                            若没有用户头像，就用 Element Plus Avatar 图标
                        -->
                        <el-avatar v-if="userInfoStore.info.userPic" :src="userInfoStore.info.userPic" />
                        <el-avatar v-else style="display: flex; align-items: center; justify-content: center;"><el-icon><Avatar /></el-icon></el-avatar>
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
                            <el-dropdown-item command="resetPassword" :icon="EditPen">重置密码</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <!-- <div style="width: 1290px; height: 570px;border: 1px solid red;">
                    内容展示区
                </div> -->
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>easy ©2025 Created by 张子凡</el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background-color: #232323;

        &__logo {
            height: 120px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: #fff;
            font-weight: bold;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-avatar {
                display: inline-flex;
                justify-content: center;
                align-items: center;
                
                .el-icon {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                }
            }

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}
</style>